package weaver.interfaces.zhlx.duss.action.purchasemodule.sap;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.SapUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-11-27
 * @Description: 需求计划变新增/更流程提交后调用新增接口(MM008)
 * @ModifyLog:
 **/
public class WeaXqjhxzbglcAddAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaXqjhxzbglcAddAction>>>>>requestid=" + requestid);
        try {
            String message = "调用SAP系统接口返回:";
            RecordSet rs = new RecordSet();
            RecordSet rs1 = new RecordSet();
            SapUtil sapUtil = new SapUtil();
            rs.executeQuery("select * from " + tablename + " where requestid=? and bglx=0", requestid);
            if (rs.next()) {
                String mainid = Util.null2String(rs.getString("id"));
                String lcbh = Util.null2String(rs.getString("lcbh"));
                String cgsqbh = Util.null2String(rs.getString("cgsqbh"));
                JSONObject dataJson = new JSONObject();
                dataJson.put("SERIAL", lcbh);
                dataJson.put("OANUM", requestid);
                dataJson.put("BANFN", cgsqbh);
                JSONArray dataitem = new JSONArray();
                rs.executeQuery("select * from " + tablename + "_dt1 where mainid=?", mainid);
                while (rs.next()) {
                    JSONObject dataitemJson = new JSONObject();
                    String cgsqdxmbh = Util.null2String(rs.getString("cgsqdxmbh"));
                    dataitemJson.put("BNFPO", cgsqdxmbh);
                    dataitemJson.put("BSART", Util.null2String(rs.getString("cgsqpzlx")));
                    dataitemJson.put("FRGKZ", Util.null2String(rs.getString("pzbs")));
                    dataitemJson.put("EKGRP", Util.null2String(rs.getString("cgz")));
                    dataitemJson.put("ERNAM", Util.null2String(rs.getString("cjdxdrymc")));
                    dataitemJson.put("ERDAT", Util.null2String(rs.getString("zhggdrq")).replace("-", ""));
                    dataitemJson.put("AFNAM", Util.null2String(rs.getString("xqz")));
                    dataitemJson.put("TXZ01", Util.null2String(rs.getString("dwb")));
                    dataitemJson.put("MATNR", Util.null2String(rs.getString("wlh")));
                    dataitemJson.put("WERKS", Util.null2String(rs.getString("gc")));
                    dataitemJson.put("LGORT", Util.null2String(rs.getString("kcdd")));
                    dataitemJson.put("BEDNR", Util.null2String(rs.getString("xqgzh")));
                    dataitemJson.put("MATKL", Util.null2String(rs.getString("wlz")));
                    dataitemJson.put("MENGE", Util.null2String(rs.getString("sl")));
                    dataitemJson.put("MEINS", Util.null2String(rs.getString("dw")));
                    dataitemJson.put("LFDAT", Util.null2String(rs.getString("xqshrq")).replace("-", ""));
                    dataitemJson.put("PREIS", Util.null2String(rs.getString("cgsqzdjg")));
                    dataitemJson.put("PEINH", Util.null2String(rs.getString("jgdw")));
                    dataitemJson.put("PSTYP", Util.null2String(rs.getString("cgpzzdxmlb")));
                    dataitemJson.put("KNTTP", Util.null2String(rs.getString("kmfplb")));
                    dataitemJson.put("EKORG", Util.null2String(rs.getString("cgzz")));
                    dataitemJson.put("KOSTL", Util.null2String(rs.getString("cbzx")));
                    dataitemJson.put("DISUB_PSPNR", Util.null2String(rs.getString("wbsxmh")));
                    JSONArray dataitemList = new JSONArray();
                    rs1.executeQuery("select * from " + tablename + "_dt3 where mainid=? and cgpzdxmbh=?", mainid, cgsqdxmbh);
                    while (rs1.next()) {
                        String zjwlbh = Util.null2String(rs1.getString("zjwlbh"));
                        String zjgc = Util.null2String(rs1.getString("zjgc"));
                        String zjjbjldw = Util.null2String(rs1.getString("zjjbjldw"));
                        String zjxqsl = Util.null2String(rs1.getString("zjxqsl"));
                        String kcdd = Util.null2String(rs1.getString("kcdd"));
                        String zjpc = Util.null2String(rs1.getString("zjpc"));
                        JSONObject dataitemJson1 = new JSONObject();
                        dataitemJson1.put("EBELP", cgsqdxmbh);
                        dataitemJson1.put("ZMATNR", zjwlbh);
                        dataitemJson1.put("ZWERKS", zjgc);
                        dataitemJson1.put("ZMEINS", zjjbjldw);
                        dataitemJson1.put("BDMNG", zjxqsl);
                        dataitemJson1.put("ZLGORT", kcdd);
                        dataitemJson1.put("CHARG", zjpc);
                        dataitemList.add(dataitemJson1);
                    }
                    dataitemJson.put("DATAITEM", dataitemList);
                    dataitem.add(dataitemJson);
                }
                dataJson.put("ITEM", dataitem);
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("jkdymc", "WeaXqjhxzbglcAddAction");
                dataMap.put("lcid", requestid);
                dataMap.put("dylc", requestid);
                dataMap.put("cz", src);
                writeLog(">>>>>WeaXqjhxzbglcAddAction>>>>>dataJson=" + dataJson);
                JSONObject resultJson = sapUtil.handleOKHttpRequest("MM008", dataJson, dataMap);
                writeLog(">>>>>WeaXqjhxzbglcAddAction>>>>>resultJson=" + resultJson);
                String code = Util.null2String(resultJson.getString("code"));
                if ("success".equals(code)) {
                    String output = Util.null2String(resultJson.getString("OUTPUT"));
                    JSONObject outputJson = JSONObject.parseObject(output);
                    String msgType = outputJson.getString("MSG_TYPE");
                    String dataStr = outputJson.getString("DATA");
                    JSONObject returnDataJson = JSONObject.parseObject(dataStr);
                    JSONArray returnList = JSONArray.parseArray(returnDataJson.getString("RETURN"));
                    if ("S".equals(msgType)) {
                        String msgText2 = "";
                        for (int i = 0; i < returnList.size(); i++) {
                            String msgType2 = returnList.getJSONObject(i).getString("MSG_TYPE");
                            if ("S".equals(msgType2)) {
                                msgText2 = returnList.getJSONObject(i).getString("MSG_TEXT2");
                                break;
                            }
                        }
                        String sql = "update " + tablename + " set cgsqbh=? where requestid=?";
                        rs.executeUpdate(sql, msgText2, requestid);
                    } else {
                        for (int i = 0; i < returnList.size(); i++) {
                            String msgType2 = returnList.getJSONObject(i).getString("MSG_TYPE");
                            if ("E".equalsIgnoreCase(msgType2)) {
                                String msgText2 = returnList.getJSONObject(i).getString("MSG_TEXT");
                                message += msgText2 + ";";
                            }
                        }
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                } else {
                    message += Util.null2String(resultJson.getString("message"));
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaXqjhxzbglcAddAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}